#!/usr/bin/env php
<?php
/**************************************************************************
 *
 *   Copyright 2010 American Public Media Group
 *
 *   This file is part of AIR2.
 *
 *   AIR2 is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   AIR2 is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with AIR2.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************/

require_once 'Test.php';
require_once 'app/init.php';
require_once 'AirHttpTest.php';
require_once 'models/TestSource.php';
require_once 'models/TestBatch.php';

// set up the table(s)
AIR2_DBManager::init();
$conn = AIR2_DBManager::get_master_connection();

// init browser
$browser = new AirHttpTest();
$browser->set_user('testuser', array(''=>false));
$browser->set_content_type(AirHttpTest::$JSON); // set to json

// create some dummy data
$b1 = new TestBatch();
$b1->save();
$b2 = new TestBatch();
$b2->save();
$s1 = new TestSource();
$s1->save();
$s2 = new TestSource();
$s1->save();

// add some annotations / addresses to the sources
$s1->SrcMailAddress[0]->smadd_primary_flag = true;
$s1->SrcMailAddress[0]->smadd_line_1 = 'TEST mail address';
$s1->SrcMailAddress[0]->smadd_city = 'Testville';
$s1->SrcMailAddress[0]->smadd_state = 'MN';
$s1->SrcMailAddress[0]->smadd_status = 'A';
$s1->SrcAnnotation[0]->srcan_type = '?';
$s1->SrcAnnotation[0]->srcan_value = 'Very good test source';
$s1->SrcAnnotation[1]->srcan_type = '?';
$s1->SrcAnnotation[1]->srcan_value = 'Extreme penmanship.';
$s1->save();
$s2->SrcMailAddress[0]->smadd_primary_flag = true;
$s2->SrcMailAddress[0]->smadd_line_1 = 'TEST second mail address';
$s2->SrcMailAddress[0]->smadd_city = 'Nowhere';
$s2->SrcMailAddress[0]->smadd_state = 'MN';
$s2->SrcMailAddress[0]->smadd_status = 'A';
$s2->SrcAnnotation[0]->srcan_type = '?';
$s2->SrcAnnotation[0]->srcan_value = 'Able to exist';
$s2->SrcAnnotation[1]->srcan_type = '?';
$s2->SrcAnnotation[1]->srcan_value = 'Interesting enough';
$s2->save();

plan(39);


/**********************
 * Add 2-sources and 1-src_annotation to a batch
 */
$uuid = $b1->batch_uuid;
$add1 = array(
    array(
        'type' => 'S',
        'reltype' => 'A',
        'uuid' => $s1->SrcAnnotation[0]->srcan_id,
    ),
    array(
        'type' => 'S',
        'uuid' => $s2->src_uuid,
    ),
);
$data1 = array('add' => json_encode($add1));
ok( $resp = $browser->http_put("/batch/$uuid/contents", $data1), 'PUT-add to batch' );
is( $browser->resp_code(), 200, "PUT-add to batch resp code" );
ok( $json = json_decode($resp,true), "JSON decode PUT-add" );
is( $json['radix']['batch_count'], 2, 'PUT-add batch count = 2' );

// GET the batch contents to determine if the annotation was really added
ok( $resp = $browser->http_get("/batch/$uuid/contents"), 'GET 1 contents' );
is( $browser->resp_code(), 200, "GET 1 contents resp code" );
ok( $json = json_decode($resp,true), "JSON decode GET 1 contents" );
is( count($json['radix']), 2, 'GET 1 count = 2' );
$b1idx = ($json['radix'][0]['Source:src_uuid'] == $s1->src_uuid) ? 0 : 1;
$b2idx = ($b1idx + 1) % 2;
is( count($json['radix'][$b1idx]['BatchRelated']), 1, 'GET 1 related count1 = 1' );
is( count($json['radix'][$b2idx]['BatchRelated']), 0, 'GET 1 related count2 = 0' );


/**********************
 * Add more items, including some overlap, to the batches
 */
$add2 = array(
    array(
        'type' => 'S',
        'reltype' => 'M',
        'uuid' => $s1->SrcMailAddress[0]->smadd_id,
    ),
    array(
        'type' => 'S',
        'reltype' => 'A',
        'uuid' => $s1->SrcAnnotation[1]->srcan_id,
    ),
    array(
        'type' => 'S',
        'reltype' => 'A',
        'uuid' => $s1->SrcAnnotation[0]->srcan_id,
    ),
    array(
        'type' => 'S',
        'reltype' => 'M',
        'uuid' => $s2->SrcMailAddress[0]->smadd_id,
    ),
);
$data2 = array('add' => json_encode($add2));
ok( $resp = $browser->http_put("/batch/$uuid/contents", $data2), 'PUT-add to batch 2' );
is( $browser->resp_code(), 200, "PUT-add to batch 2 resp code" );
ok( $json = json_decode($resp,true), "JSON decode PUT-add 2" );
is( $json['radix']['batch_count'], 2, 'PUT-add batch 2 count = 2' );

// GET the batch contents to determine if the related counts are correct
ok( $resp = $browser->http_get("/batch/$uuid/contents"), 'GET 2 contents' );

is( $browser->resp_code(), 200, "GET 2 contents resp code" );
ok( $json = json_decode($resp,true), "JSON decode GET 2 contents" );
is( count($json['radix']), 2, 'GET 2 count = 2' );
$b1idx = ($json['radix'][0]['Source:src_uuid'] == $s1->src_uuid) ? 0 : 1;
$b2idx = ($b1idx + 1) % 2;
is( count($json['radix'][$b1idx]['BatchRelated']), 3, 'GET 2 related count1 = 3' );
is( count($json['radix'][$b2idx]['BatchRelated']), 1, 'GET 2 related count2 = 1' );


/**********************
 * Remove a batch_related, WITHOUT removing the batch item
 */
$rmv3 = array(
    array(
        'type' => 'S',
        'reltype' => 'A',
        'uuid' => $s1->SrcAnnotation[1]->srcan_id,
    ),
    array(
        'type' => 'S',
        'reltype' => 'M',
        'uuid' => $s2->SrcMailAddress[0]->smadd_id,
    ),
);
$data3 = array('remove' => json_encode($rmv3));
ok( $resp = $browser->http_put("/batch/$uuid/contents", $data3), 'PUT-remove 3 to batch' );
is( $browser->resp_code(), 200, "PUT-remove 3 to batch resp code" );
ok( $json = json_decode($resp,true), "JSON decode PUT-remove 3" );
is( $json['radix']['batch_count'], 2, 'PUT-remove 3 batch count = 2' );

// GET the batch contents to determine if the related were deleted
ok( $resp = $browser->http_get("/batch/$uuid/contents"), 'GET 3 contents' );
is( $browser->resp_code(), 200, "GET 3 contents resp code" );
ok( $json = json_decode($resp,true), "JSON decode GET 3 contents" );
is( count($json['radix']), 2, 'GET 3 count = 2' );
$b1idx = ($json['radix'][0]['Source:src_uuid'] == $s1->src_uuid) ? 0 : 1;
$b2idx = ($b1idx + 1) % 2;
is( count($json['radix'][$b1idx]['BatchRelated']), 2, 'GET 3 related count1 = 2' );
is( count($json['radix'][$b2idx]['BatchRelated']), 0, 'GET 3 related count2 = 0' );


/**********************
 * Remove a batch_item and a batch_related, and a non-existent batch_related
 */
$rmv4 = array(
    array(
        'type' => 'S',
        'reltype' => 'M',
        'uuid' => $s1->SrcMailAddress[0]->smadd_id,
    ),
    array(
        'type' => 'S',
        'reltype' => 'M',
        'uuid' => $s2->SrcMailAddress[0]->smadd_id,
    ),
    array(
        'type' => 'S',
        'uuid' => $s1->src_uuid,
    ),
);
$data4 = array('remove' => json_encode($rmv4));
ok( $resp = $browser->http_put("/batch/$uuid/contents", $data4), 'PUT-remove 4 to batch' );
is( $browser->resp_code(), 200, "PUT-remove 4 to batch resp code" );
ok( $json = json_decode($resp,true), "JSON decode PUT-remove 4" );
is( $json['radix']['batch_count'], 1, 'PUT-remove 4 batch count = 1' );

// GET the batch contents
ok( $resp = $browser->http_get("/batch/$uuid/contents"), 'GET 4 contents' );
is( $browser->resp_code(), 200, "GET 4 contents resp code" );
ok( $json = json_decode($resp,true), "JSON decode GET 4 contents" );
is( count($json['radix']), 1, 'GET 4 count = 1' );
is( count($json['radix'][0]['BatchRelated']), 0, 'GET 4 related count1 = 0' );


?>
